package baseserverframe;

import javax.swing.*;



//------------------------------------------------------------------------------
import java.util.Vector;







/*******************************************************************************
 *
 * Document class.<br>
 *
 * It contains different data and view classes for visualization of that data.
 * It also contains status bars. Class has methods for manipulation with views,
 * methods to register new view, delete old. There can be only one active view
 * at the moment.
 *
 * @author Truewar Ashman(<a href="mailto:truewar@hotmail.ru">truewar@hotmail.ru</a>) ID = 2009
 * @version 0.3<br>
 * @see view
 * @see mainFrame
 */

public class document
{
// +Variables for doc.----------------------------------------------------------
	private mainFrame frameLink;
	/** Name of the current document */
	private String	docName;
// -Variables for doc.----------------------------------------------------------
// +Variables for view.---------------------------------------------------------
	/** Views container */
	private Vector <view> viewContainer;
	/** Status bars container */
	private Vector <status> statContainer;
	/** Current visible view */
	private int currentView;
// -Variables for view.---------------------------------------------------------
	/**
	 * Class for status bars. It contains JLabel and name for that status bar.
	 *
	 * @author Truewar Ashman(<a href="mailto:truewar@hotmail.ru">truewar@hotmail.ru</a>)
	 * @version 0.3<br>
	 */
	private class status
    {
		/** Logical name of element */
		private String		name;
		/** Link to JLabel element */
        private JLabel	component;

		public status(String name, JLabel component)
		{
			this.name		= name;
			this.component	= component;
		}
    };
/**
 *  Class constructor<br>
 */
	public document()
	{
		viewContainer = new Vector <view>();
		statContainer = new Vector <status>();
		currentView = -1;
	}
// +Add methods ----------------------------------------------------------------
/**
 *  Method to add view to document<br>
 *
 *	@param name logical name of view.
 *  @param newView link to view.
 *  @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>true</b> - view was successfully added.
 *			<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>false</b> - view with that name was already registered.
 */
	final public boolean addView(String name, view newView)
	{
		int cur = getViewIdByName(name);
		if(cur != -1) return false;

		newView.reregister(docName, name);
		viewContainer.add(newView);
		newView.setDocLink(this);
		return true;
	}

/**
 *  Method to add status bar to document.<br>
 *
 *	@param name logical name of status bar.
 *  @param newStatus link to status bar.
 *  @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>true</b> - status was successfully added.
 *			<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>false</b> - status with that name was already registered.
 */
	final public boolean addStatus(String name, JLabel newStatus)
	{
		int cur = getStatusIdByName(name);
		if(cur != -1) return false;

		statContainer.add(new status(name, newStatus));
		return true;
	}
// -Add methods ----------------------------------------------------------------
// +Remove methods -------------------------------------------------------------
/**
 *  Method to delete view from document by name.<br>
 *
 *	@param name logical name of view.
 *  @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>true</b> - view was successfully removed.
 *			<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>false</b> - there are no view with such name.
 */
	final public boolean removeView(String name)
	{
		int rem = getViewIdByName(name);
		if(rem == -1) return false;

		viewContainer.remove(rem);
		currentView = -1;
		return true;
	}

/**
 *  Method to delete view from document by index.<br>
 *
 *	@param id index of view.
 *  @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>true</b> - view was successfully removed.
 *			<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>false</b> -  there are no view with such index.
 */
	final public boolean removeView(int id)
	{
		if((id < 0)||(id > viewContainer.size()))
			return false;

		viewContainer.remove(id);
		currentView = -1;
		return true;
	}

/**
 *  Method to delete status from document by name.<br>
 *
 *	@param name logical name of status.
 *  @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>true</b> - status was successfully removed.
 *			<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>false</b> -  there are no status with such name.
 */
	final public boolean removeStatus(String name)
	{
		int rem = getStatusIdByName(name);
		if(rem == -1) return false;

		statContainer.remove(rem);
		return true;
	}

/**
 *  Method to delete status from document by index.<br>
 *
 *	@param id index of status.
 *  @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>true</b> - status was successfully removed.
 *			<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>false</b> -  there are no status with such index.
 */
	final public boolean removeStatus(int id)
	{
		if((id < 0)||(id > statContainer.size()))
			return false;
		statContainer.remove(id);
		return true;
	}
// +Remove methods -------------------------------------------------------------
/**
 *  Method to set current view for document by name.<br>
 *
 *	@param name logical name of view.
 *  @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>true</b> - view was successfully set.
 *			<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>false</b> - there are no view with such name.
 */
	final public boolean setCurrentView(String name)
	{
		int cur = getViewIdByName(name);
		if(cur == -1) return false;

		currentView = cur;
		return true;
	}

/**
 *  Method to set current view for document by index.<br>
 *
 *	@param id index of view.
 *  @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>true</b> - view was successfully set.
 *			<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>false</b> - there are no view with such index.
 */

	final public boolean setCurrentView(int id)
	{
		if((id < 0)||(id > viewContainer.size()))
			return false;
		currentView = id;
		return true;
	}

// +set-------------------------------------------------------------------------
/**
 *  Method to set name of document.<br>
 *
 *	@param docName new name of document.
 */
	final public void setDocName(String docName)
	{
		this.docName = docName;
	}

/**
 * 
 * @param frameLink link to frame.
 */
	final public void setFrameLink(mainFrame frameLink)
	{
		this.frameLink = frameLink;
	}
// -set-------------------------------------------------------------------------
// +get-------------------------------------------------------------------------
/**
 *  Returns name of document.<br>
 *
 * @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>String</b> - name of document.
 */
	final public String getDocName()
	{
		return docName;
	}

/**
 *  Returns link to view with specified name.<br>
 *
 * @param name name of view.
 * @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>view</b> - link to view.
 */
	final public view getView(String name)
	{
		int cur = getViewIdByName(name);
		if(cur == -1) return null;

		return viewContainer.get(cur);
	}
/**
 *  Returns link to view with specified index.<br>
 *
 * @param id index of view.
 * @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>view</b> - link to view.
 */
	final public view getView(int id)
	{
		if((id < 0)||(id > viewContainer.size()))
			return null;
		return viewContainer.get(id);
	}

/**
 *  Returns number of views in document.<br>
 *
 * @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>int</b> - number of views.
 */
	final public int getNumOfView()
	{
		return viewContainer.size();
	}

/**
 *  Returns link to current view.<br>
 *
 * @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>view</b> - link to view.
 */
	final public view getCurrentView()
	{
		if(currentView == -1) return null;
		return viewContainer.get(currentView);
	}

/**
 *  Returns link to status by name.<br>
 *
 * @param name logical name of status.
 * @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>view</b> - link to status.
 */
	final public JLabel getStatus(String name)
	{
		int cur = getStatusIdByName(name);
		if(cur == -1) return null;

		return statContainer.get(cur).component;
	}

/**
 *  Returns link to status by index.<br>
 *
 * @param id index of status.
 * @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>view</b> - ссылка на статус.
 */
	final public JLabel getStatus(int id)
	{
		if((id < 0)||(id > statContainer.size()))
			return null;
		return statContainer.get(id).component;
	}

/**
 *  Returns number of status.<br>
 *
 * @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>int</b> - number of status.
 */
	final public int getNumOfStat()
	{
		return statContainer.size();
	}

/**
 * 
 * @return link to frame.
 */
	final public mainFrame getFrameLink()
	{
		return frameLink;
	}
// -get-------------------------------------------------------------------------
// +private---------------------------------------------------------------------
/**
 *  Method to get index of view by name.<br>
 *	
 * @param name logical name of view.
 * @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>int</b> - index of specified view.
 */
	final private int getViewIdByName(String name)
	{
		for(int i = 0; i < viewContainer.size(); i++)
		{
			if(viewContainer.get(i).getViewName().compareTo(name) == 0)
				return i;
		}
		return -1;
	}

/**
 * Method to get index of status by name.<br>
 *
 * @param name logical name of status.
 * @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>int</b> - index of specified status.
 */
	final private int getStatusIdByName(String name)
	{
		for(int i = 0; i < statContainer.size(); i++)
		{
			if(statContainer.get(i).name.compareTo(name) == 0)
				return i;
		}
		return -1;
	}
// -private---------------------------------------------------------------------
}
